package com.c1202m.dao;

import codeblue.project.MD5Encode;
import static com.c1202m.dao.BangGiaoDichDAO.kiemTraTaiKhoan;
import com.c1202m.gui.CustomMessageDialog;
import com.c1202m.hibernate.entities.TaiKhoan;
import java.util.List;
import java.util.Random;
import java.util.Vector;
import org.hibernate.Query;
import org.hibernate.Session;

public class BangGiaoDichDAO {

    public static Vector layToanBoDoiTuong() {
        Vector vt = new Vector();
        Session session = DangNhapDAO.sessionFactory.openSession();
        try {
            session.getTransaction().begin();
            List list = session.createQuery("from TaiKhoan where trangThai = TRUE").list();
            for (Object obj : list) {
                TaiKhoan taiKhoan = (TaiKhoan) obj;
                Vector temp = new Vector();
                temp.add(taiKhoan.getSoTK());
                temp.add(taiKhoan.getChuTK());
                vt.add(temp);
            }
            session.getTransaction().commit();
            session.close();
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
        }
        return vt;
    }

    public static TaiKhoan layMotDoiTuong(String id) {
        TaiKhoan taiKhoan = null;
        Session session = DangNhapDAO.sessionFactory.openSession();
        try {
            session.getTransaction().begin();
            taiKhoan = (TaiKhoan) session.get(TaiKhoan.class, id);
            session.getTransaction().commit();
            session.close();
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
        }
        return taiKhoan;
    }

    public static boolean them(String soTK, String chuTK, String soCMND, String diaChi, String dienThoai, String email, double soDuTK, boolean trangThai) {
        if (!kiemTraTaiKhoan(soTK)) {
            new CustomMessageDialog(null, true, "Oops..!", "Object exists", CustomMessageDialog.MESSAGE);
            return false;
        }
        Session session = DangNhapDAO.sessionFactory.openSession();
        try {
            session.getTransaction().begin();
            TaiKhoan taiKhoan = new TaiKhoan();
            taiKhoan.setSoTK(soTK);
            taiKhoan.setMaPin(MD5Encode.encode("123456"));
            taiKhoan.setChuTK(chuTK);
            taiKhoan.setSoCMND(soCMND);
            taiKhoan.setDiaChi(diaChi);
            taiKhoan.setDienThoai(dienThoai);
            taiKhoan.setEmail(email);
            taiKhoan.setSoDuTK(soDuTK);
            taiKhoan.setTrangThai(trangThai);
            session.save(taiKhoan);
            session.getTransaction().commit();
            session.close();
            return true;
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
        }
        new CustomMessageDialog(null, true, "Oops...!", "An error occured!", CustomMessageDialog.MESSAGE);
        return false;
    }

    public static boolean sua(String soTK, String chuTK, String soCMND, String diaChi, String dienThoai, String email, double soDuTK, boolean trangThai) {
        Session session = DangNhapDAO.sessionFactory.openSession();
        try {
            session.getTransaction().begin();
            TaiKhoan taiKhoan = (TaiKhoan) session.get(TaiKhoan.class, soTK);
            taiKhoan.setChuTK(chuTK);
            taiKhoan.setSoCMND(soCMND);
            taiKhoan.setDiaChi(diaChi);
            taiKhoan.setDienThoai(dienThoai);
            taiKhoan.setEmail(email);
            taiKhoan.setSoDuTK(soDuTK);
            taiKhoan.setTrangThai(trangThai);
            session.update(taiKhoan);
            session.getTransaction().commit();
            session.close();
            return true;
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
        }
        new CustomMessageDialog(null, true, "Oops...!", "An error occured!", CustomMessageDialog.MESSAGE);
        return false;
    }

    public static boolean xoa(String id) {
        Session session = DangNhapDAO.sessionFactory.openSession();
        try {
            session.getTransaction().begin();
            TaiKhoan taiKhoan = (TaiKhoan) session.get(TaiKhoan.class, id);
            session.delete(taiKhoan);
            session.getTransaction().commit();
            session.close();
            return true;
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
        }
        new CustomMessageDialog(null, true, "Oops...!", "An error occured!", CustomMessageDialog.MESSAGE);
        return false;
    }

    public static boolean layLaiMaPin(String id) {
        Session session = DangNhapDAO.sessionFactory.openSession();
        try {
            session.getTransaction().begin();
            TaiKhoan taiKhoan = (TaiKhoan) session.get(TaiKhoan.class, id);
            taiKhoan.setMaPin(MD5Encode.encode("123456"));
            session.update(taiKhoan);
            session.getTransaction().commit();
            session.close();
            return true;
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
        }
        new CustomMessageDialog(null, true, "Oops...!", "An error occured!", CustomMessageDialog.MESSAGE);
        return false;
    }

    public static boolean kiemTraTaiKhoan(String soTK) {
        List list = null;
        Session session = DangNhapDAO.sessionFactory.openSession();
        try {
            session.getTransaction().begin();
            Query query = session.createQuery("from TaiKhoan a where a.soTK = ?");
            query.setString(0, soTK);
            list = query.list();
            session.getTransaction().commit();
            session.close();
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
        }
        return list.isEmpty() ? true : false;
    }

    private static String random() {
        Random generator = new Random();
        String str = "";
        for (int i = 0; i < 4; i++) {
            str += Integer.toString(generator.nextInt(10));
        }
        return str;
    }

    public static String laySoTKNgauNhien() {
        String strNum = "";
        strNum = random() + "-" + random() + "-" + random() + "-" + random();
        return strNum;
    }
}